package com.jsbs.iam.ident.service.impl;

import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.param.AccountInfoDto;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.ident.feign.IdentFeignInter;
import com.jsbs.iam.ident.mapper.UserMapper;
import com.jsbs.iam.ident.param.UserQueryParam;
import com.jsbs.iam.ident.param.UserQueryResp;
import com.jsbs.iam.ident.service.IdentityService;
import com.jsbs.iam.ident.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName: UserServiceImpl
 * @Author: tenghuawei
 * @Date: 2022/12/13 11:18
 * @Description:
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    IdentFeignInter identFeignInter;
    @Autowired
    IdentityService identityService;

    /**
     * 功能描述：1、用户中心——用户列表分页查询（只展示没有开通过账号的用户）
     * @param userQueryParam
     * @return Result
     */
    @Override
    public PageInfo queryForUser(UserQueryParam userQueryParam) {
        //初始化分页数据
        Result<PageInfo<UserQueryResp>> result = identFeignInter.queryUserNotOpen(userQueryParam);
        log.info("Route=>UserServiceImpl method=>queryForUser 远程openfeign调用user模块后，返回结果result为：{} ", result);
        PageInfo<UserQueryResp> initPageInfo = result.getData();
        if(Objects.isNull(initPageInfo)){
            return new PageInfo();
        }
        return initPageInfo;
    }

    /**
     * 功能描述：2、根据用户编码（usercode工号）,开通账号
     * @param accountInfoDto
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean openAccount(List<AccountInfoDto> accountInfoDto) {
        try {
            List<String> userCodeList = identityService.accountCreate(accountInfoDto);
            Result result = identFeignInter.openAccount(userCodeList);
            if(0==result.getCode()){
                log.info("Route=>UserServiceImpl method=>openAccount 用户开通账户成功，远程调用返回结果为：{} ", Json.toJsonString(result));
                return true;
            }else {
                throw new RuntimeException("开通账号失败！");
            }
        }catch (Exception e) {
            log.error("Route=>UserServiceImpl method=>openAccount 用户开通账户异常：{}",e.getMessage());
            throw new RuntimeException("用户开通账户异常："+e.getMessage());
        }
    }





}
